package com.billing.mvc.views;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.billing.persistance.entities.BillingEntry;
import com.billing.persistance.entities.Invoice;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfCell;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.draw.LineSeparator;

public class PdfInvoice extends AbstractPdfView{

    @Override
    public void buildPdfDocument(Map<String, Object> model,Document document, PdfWriter writer, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        final DecimalFormat df = new DecimalFormat("0.00");
    	
        if (model.containsKey("invoice")){
            Invoice invoice = (Invoice) model.get("invoice");
            
            
            LineSeparator line = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, 0);
            
            Paragraph header = new Paragraph(20);
            header.add(new Chunk("Factura [" + invoice.getId() + "]"));
            header.add(line);
            header.setSpacingAfter(30);
            document.add(header);
            
            Paragraph description = new Paragraph(10);
            description.add("Resumen: " + invoice.getDescription());
            document.add(description);
            
            Paragraph payee = new Paragraph(50);
            payee.add("Beneficiario: " + invoice.getPayee());
            document.add(payee);
            
            Paragraph payer = new Paragraph(50);
            payer.add("Pagador: " + invoice.getPayer());
            document.add(payer);

            document.add(line);
            
            // Render the entries
            PdfPTable table = new PdfPTable(4);
            table.addCell("Fecha");
            table.addCell("Categoria");
            table.addCell("Descripcion");
            table.addCell("Cantidad");
     
            PdfPCell amountCell;
            
            for (BillingEntry entry : invoice.getBillingEntries()) {
            	table.addCell(sdf.format(entry.getDate()));
                table.addCell(entry.getConcept() != null ? entry.getDescription() : "N/A");
                table.addCell(entry.getDescription());
                
                amountCell = new PdfPCell(new Phrase(df.format(entry.getAmount())));
                amountCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(amountCell);
            }
            
            PdfPCell totalCell = new PdfPCell(new Phrase("Total: " + df.format(invoice.getTotalAmount())));
            totalCell.setColspan(4);
            totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(totalCell);
            
            document.add(table);
        }
        
        
    }
    

}

